package com.lh.blogResource.service.impl;

import com.lh.blogCommon.util.PageUtils;
import com.lh.blogCommon.util.Query;
import com.lh.blogResource.entity.ResourcesEntity;
import com.lh.blogResource.feign.OssFeignService;
import com.lh.blogResource.mapper.ResourcesMapper;
import com.lh.blogResource.service.ResourcesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.StringUtils;


@Service("resourcesService")
public class ResourcesServiceImpl extends ServiceImpl<ResourcesMapper, ResourcesEntity> implements ResourcesService {

    @Autowired
    OssFeignService ossFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<ResourcesEntity> queryWrapper = new QueryWrapper<>();

        String key = (String) params.get("key");

        if(!StringUtils.isEmpty(key)){
            queryWrapper.eq("resources_id",key).or()
                    .eq("resourcetype_id",key).or()
                    .eq("resources_name",key).or()
                    .eq("resources_link",key).or()
                    .eq("resources_img",key).or()
                    .eq("creation_time",key);
        }
        queryWrapper.orderByDesc("resources_id");

        IPage<ResourcesEntity> page = this.page(
                new Query<ResourcesEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void removeAdImageByIds(String[] resourcesIds) {
        List<String> imgs = baseMapper.selectBatchIds(Arrays.asList(resourcesIds)).stream().map((item) -> {
            return item.getResourcesImg();
        }).collect(Collectors.toList());

        ossFeignService.removeList(imgs);
    }

}